public class JosephRing {
    public static void main(String[] args) {
        System.out.println(josephRing(18, 3) + 1);
        System.out.println(josephRing(5, 3) + 1);
    }

    public static int josephRing(int n, int m) {
        if (n == 1) {
            return 0;
        } else {
            return (josephRing(n - 1, m) + m) % n;
        }
    }
}
